﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data_DAO_DTO_Lib.DTO;
using N03_11HCA_HTTTHD.Models;
namespace N03_11HCA_HTTTHD.Controllers

{
    public class KhoController : Controller
    {
        // GET: /Kho/
        Data_DAO_DTO_Lib.LINHVUC_DAO linhVucDAO = new Data_DAO_DTO_Lib.LINHVUC_DAO();
        Data_DAO_DTO_Lib.DANHMUC_DAO danhMucDAO = new Data_DAO_DTO_Lib.DANHMUC_DAO();
        Data_DAO_DTO_Lib.CHUYENGIAO_DAO chuyenGiaoDAO = new Data_DAO_DTO_Lib.CHUYENGIAO_DAO();
        Data_DAO_DTO_Lib.CUNGCAP_DAO cungCapDAO = new Data_DAO_DTO_Lib.CUNGCAP_DAO();
        Data_DAO_DTO_Lib.MUCDOPHATTRIENTHIETBI_DAO mucDoPhatTrienDAO = new Data_DAO_DTO_Lib.MUCDOPHATTRIENTHIETBI_DAO();
        Data_DAO_DTO_Lib.XUATXUDETAI_DAO xuatXuDeTaiDAO = new Data_DAO_DTO_Lib.XUATXUDETAI_DAO();
        Data_DAO_DTO_Lib.TIEUCHUANTHIETBI_DAO tieuChuanDAO = new Data_DAO_DTO_Lib.TIEUCHUANTHIETBI_DAO();

        Data_DAO_DTO_Lib.THONGTINTIMMUA_DAO timmuaDAO = new Data_DAO_DTO_Lib.THONGTINTIMMUA_DAO();

        Data_DAO_DTO_Lib.RAOVAT_DAO raovatDAO = new Data_DAO_DTO_Lib.RAOVAT_DAO();

        public ActionResult Index()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            return View("Kho"); 
        }
        //
        // POST: /Admin/XoaCanBan
        [HttpPost]
        public JsonResult XoaCanBan(int id)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);
            if (isCoQuyen == false)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền thực thi tác vụ này!" });
            }
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            
            //kiểm tra thêm người xoá
            try
            {
                canBanDAO.Xoa(id, (int)TK.ID1);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        //
        // POST: /Admin/XoaRaoVat
        [HttpPost]
        public JsonResult XoaTimMua(int id)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemTimMua);
            if (isCoQuyen == false)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền thực thi tác vụ này!" });
            }

            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            try
            {
                timmuaDAO.Xoa(id, (int)TK.ID1);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        //
        // POST: /Admin/XoaRaoVat
        [HttpPost]
        public JsonResult XoaRaoVat(int id)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemRaoVat);
            if (isCoQuyen == false)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền thực thi tác vụ này!" });
            }

            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            try
            {
                raovatDAO.Xoa(id, (int)TK.ID1);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        public ActionResult DanhSachChaoBan()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }

            Models.ThongTinDanhNhapTaiKhoan tk = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            ViewBag.TK = tk;

            List<Data_DAO_DTO_Lib.DTO.DTO> list = canBanDAO.LayDanhSachCoThamSo((int)tk.ID1, 0, N03_11HCA_HTTTHD.TienIch.HeThong.SoLuongLoadCanBanMacDinh, N03_11HCA_HTTTHD.TienIch.HeThong.ChuoiSapXepCanBanMacDinh);

            Models.Kho_DanhSachChaoBan model = new Models.Kho_DanhSachChaoBan(list);

            return View("Kho_DanhSachChaoBan", model);
        }

        public ActionResult ChaoBanQuanTam()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }

            N03_11HCA_HTTTHD.Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            ViewBag.TK = TK;

            List<Data_DAO_DTO_Lib.DTO.DTO> list = canBanDAO.LayChaoBanQuanTamCoThamSo((int)TK.ID1, 0, N03_11HCA_HTTTHD.TienIch.HeThong.SoLuongLoadCanBanMacDinh, N03_11HCA_HTTTHD.TienIch.HeThong.ChuoiSapXepCanBanMacDinh);

            Models.Kho_DanhSachChaoBan model = new Models.Kho_DanhSachChaoBan(list);

            return View("Kho_ChaoBanQuanTam", model);
        }

        Data_DAO_DTO_Lib.THONGTINCANBAN_DAO canBanDAO = new Data_DAO_DTO_Lib.THONGTINCANBAN_DAO();

        public ActionResult DanhSachTimMua()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemTimMua);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            Models.ThongTinDanhNhapTaiKhoan tk = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            ViewBag.TK = tk;


            List<Data_DAO_DTO_Lib.DTO.DTO> list = timmuaDAO.LayDanhSachCoThamSo((int)tk.ID1, 0, N03_11HCA_HTTTHD.TienIch.HeThong.SoLuongLoadTimMuaMacDinh, N03_11HCA_HTTTHD.TienIch.HeThong.ChuoiSapXepTimMuaMacDinh);

            Models.Kho_DanhSachTimMua model = new Models.Kho_DanhSachTimMua(list);

            return View("Kho_DanhSachTimMua", model);

        }

        public ActionResult TimMuaQuanTam()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            N03_11HCA_HTTTHD.Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            ViewBag.TK = TK;

            List<Data_DAO_DTO_Lib.DTO.DTO> list = timmuaDAO.LayTimMuaQuanTamCoThamSo((int)TK.ID1, 0, N03_11HCA_HTTTHD.TienIch.HeThong.SoLuongLoadTimMuaMacDinh, N03_11HCA_HTTTHD.TienIch.HeThong.ChuoiSapXepTimMuaMacDinh);

            Models.Kho_DanhSachTimMua model = new Models.Kho_DanhSachTimMua(list);

            return View("Kho_TimMuaQuanTam", model);

        }

        public ActionResult RaoVatQuanTam()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemRaoVat);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            N03_11HCA_HTTTHD.Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            ViewBag.TK = TK;


            List<Data_DAO_DTO_Lib.DTO.DTO> list = raovatDAO.LayRaoVatQuanTamCoThamSo((int)TK.ID1, 0, N03_11HCA_HTTTHD.TienIch.HeThong.SoLuongLoadRaoVatMacDinh, N03_11HCA_HTTTHD.TienIch.HeThong.ChuoiSapXepRaoVatMacDinh);

            Models.Kho_DanhSachRaoVat model = new Models.Kho_DanhSachRaoVat(list);

            return View("Kho_RaoVatQuanTam", model);

        }

        public ActionResult DanhSachRaoVat()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemRaoVat);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            N03_11HCA_HTTTHD.Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            ViewBag.TK = TK;

            List<Data_DAO_DTO_Lib.DTO.DTO> list = raovatDAO.LayDanhSachCoThamSo(0, N03_11HCA_HTTTHD.TienIch.HeThong.SoLuongLoadRaoVatMacDinh, N03_11HCA_HTTTHD.TienIch.HeThong.ChuoiSapXepRaoVatMacDinh);

            Models.Kho_DanhSachRaoVat model = new Models.Kho_DanhSachRaoVat(list);

            return View("Kho_DanhSachRaoVat", model);

        }

        public ActionResult ThemTimMua()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemTimMua);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> lTieuChuan = tieuChuanDAO.LayDanhSach();

            List<Data_DAO_DTO_Lib.DTO.DTO> lChuyenGiao = chuyenGiaoDAO.LayDanhSach();

            List<Data_DAO_DTO_Lib.DTO.DTO> lPhanLoai = danhMucDAO.LayDanhSach();

            Models.Kho_ThemTimMua model = new Models.Kho_ThemTimMua(lTieuChuan, lChuyenGiao,lPhanLoai);
            return View("Kho_ThemTimMua",model);
        }

        [HttpPost]
        public ActionResult ThemTimMua(FormCollection mangGiaTri)
        {

            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemTimMua);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            N03_11HCA_HTTTHD.Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            THONGTINTIMMUA_DTO thongtintim = new THONGTINTIMMUA_DTO();
            thongtintim.LoaiCanBan = int.Parse(mangGiaTri[Kho_ThemTimMua.Name_loaiTimMua]);
            thongtintim.MoTa = mangGiaTri[Kho_ThemTimMua.Name_moTa];
            thongtintim.SanXuatSauNam =int.Parse( mangGiaTri[Kho_ThemTimMua.Name_namSanXuat]);
            thongtintim.TenAnh = mangGiaTri[Kho_ThemTimMua.Name_tenAnh];
            thongtintim.TenViet = mangGiaTri[Kho_ThemTimMua.Name_tenViet];
            thongtintim.ThongTinKhac = mangGiaTri[Kho_ThemTimMua.Name_thongTinKhac];
            thongtintim.TuKhoa = mangGiaTri[Kho_ThemTimMua.Name_tuKhoa];
            thongtintim.XuatXu = mangGiaTri[Kho_ThemTimMua.Name_xuatXu];

            thongtintim.IDNguoiDang = (int)TK.ID1;

            
            //thongtintim.      :       tiêu chuẩn
            String[] mangTieuChuan = mangGiaTri.GetValues(Kho_ThemTimMua.Name_tieuChuan);
            List<int> listTieuChuan = new List<int>();
            foreach (String tieuChuan in mangTieuChuan)
            {
                listTieuChuan.Add( Int32.Parse(tieuChuan));
            }

            String[] mangChuyenGiao = mangGiaTri.GetValues(Kho_ThemTimMua.Name_chuyenGiao);
            List<int> listChuyenGiao = new List<int>();
            foreach (String chuyenGiao in mangChuyenGiao)
            {
                listChuyenGiao.Add(Int32.Parse(chuyenGiao));
            }

            String[] mangLinhVuc = mangGiaTri.GetValues(Kho_ThemTimMua.Name_linhVuc);
            List<int> listLinhVuc = new List<int>();
            foreach (String linhVuc in mangLinhVuc)
            {
                listLinhVuc.Add(Int32.Parse(linhVuc));
            }

            //thongtintim.      :       chuyển giao
            timmuaDAO.Them(thongtintim, listChuyenGiao, listLinhVuc, listTieuChuan);
            return DanhSachTimMua();
        }

        public ActionResult ThemRaoVat()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemRaoVat);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);


            return View("Kho_ThemRaoVat");
        }
        [HttpPost]
        public ActionResult ThemRaoVat(FormCollection mangGiaTri)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemRaoVat);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }

            Models.ThongTinDanhNhapTaiKhoan tk= N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            RAOVAT_DTO raovat = new RAOVAT_DTO();
            raovat.ChuDe = mangGiaTri[Kho_ThemRaoVat.Name_chuDe];
            raovat.NoiDung = mangGiaTri[Kho_ThemRaoVat.Name_noiDung];
            raovat.IDTaiKhoanRaoVat = (int)tk.ID1;
            raovatDAO.Them(raovat);
            return DanhSachRaoVat();
        }

        public ActionResult Them_ThietBiCongNghe()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listLinhVuc = linhVucDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listChuyenGiao = chuyenGiaoDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listCungCap = cungCapDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listMucDoPhatTrien = mucDoPhatTrienDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listXuatXuDeTai = xuatXuDeTaiDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listTieuChuan = tieuChuanDAO.LayDanhSach();
            Models.Kho_Them_ThietBiCongNghe model = new Models.Kho_Them_ThietBiCongNghe(listPhanLoai, listLinhVuc, listChuyenGiao, listCungCap, listMucDoPhatTrien, listXuatXuDeTai, listTieuChuan);
            return View("Kho_Them_ThietBiCongNghe", model);
        }

        public ActionResult Them_DichVu()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listLinhVuc = linhVucDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listChuyenGiao = chuyenGiaoDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listCungCap = cungCapDAO.LayDanhSach();
            Models.ChaoBan_Them_DichVu model = new Models.ChaoBan_Them_DichVu(listPhanLoai, listLinhVuc, listChuyenGiao, listCungCap);
            return View("Kho_Them_DichVu", model);
        }

        [HttpPost]
        public ActionResult Them_DichVu(FormCollection mangInput)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }

            N03_11HCA_HTTTHD.Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            THONGTINCANBAN_DTO dv = new THONGTINCANBAN_DTO();
            //loai can ban: THietBiCongNghe = 1, DichVu = 2, GIaiPhapPhanMem = 3
            dv.LoaiCanBan = 2;
            dv.IDNguoiDang = (int)TK.ID1;
            //dv.MucDoPhatTrien = 
            dv.TenAnh = mangInput[Models.ChaoBan_Them_DichVu.Name_tenAnh];
            dv.TenViet = mangInput[Models.ChaoBan_Them_DichVu.Name_tenViet];
            dv.XuatXu = mangInput[Models.ChaoBan_Them_DichVu.Name_sllXuatXu];
            dv.MoTa = mangInput[Models.ChaoBan_Them_DichVu.Name_txtMoTa];
            dv.UuDiem = mangInput[Models.ChaoBan_Them_DichVu.Name_txtUuDiem];
            dv.HinhAnhDinhKem = "abc";
            dv.TepTinDinhKem = "abc";

            //Phần thông tin dịch vụ
            
            DICHVUCANBAN_DTO dvcb = new DICHVUCANBAN_DTO();
            dvcb.DoiTuongSuDung = mangInput[Models.ChaoBan_Them_DichVu.Name_txtDoiTuongSuDung];

            //phần chuyển giao           
            String[] mangChuyenGiao = mangInput.GetValues(Models.ChaoBan_Them_DichVu.Name_chuyenGiao);
            List<int> listChuyenGiao = new List<int>();
            foreach (String chuyenGiao in mangChuyenGiao)
            {
                listChuyenGiao.Add(Int32.Parse(chuyenGiao));
            }

            String[] mangCungCap = mangInput.GetValues(Models.ChaoBan_Them_DichVu.Name_cungCap);
            List<int> listCungCap = new List<int>();
            foreach (String cungCap in mangCungCap)
            {
                listCungCap.Add(Int32.Parse(cungCap));
            }

            THONGTINCHUYENGIAOCANBAN_DTO ttcg = new THONGTINCHUYENGIAOCANBAN_DTO();
            ttcg.BaoHanh = mangInput[Models.ChaoBan_Them_DichVu.Name_baoHanh];
            ttcg.ChiPhiKhac = mangInput[Models.ChaoBan_Them_DichVu.Name_chiPhiKhac];
            ttcg.DanhSachDonViChuyenGiao = mangInput[Models.ChaoBan_Them_DichVu.Name_chuyenGiaoCho];
            ttcg.GiaKhoiDiem  = mangInput[Models.ChaoBan_Them_DichVu.Name_giaKhoiDiem] + " - "+mangInput[Models.ChaoBan_Them_DichVu.Name_giaKetThuc];;
            
            //a = mangInput[Models.ChaoBan_Them_DichVu.Name_donviTinh];
            //a = mangInput[Models.ChaoBan_Them_DichVu.Name_donviTienTe];
            //a = mangInput[Models.ChaoBan_Them_DichVu.Name_txtDieuKienKhac];

            dv.TuKhoa = mangInput[Models.ChaoBan_Them_DichVu.Name_txttuKhoa];

            //thông tin phân loại
            String[] mangDanhMuc = mangInput.GetValues(Models.ChaoBan_Them_DichVu.Name_slPhanLoai);
            List<int> listDanhMuc = new List<int>();
            foreach (String DanhMuc in mangDanhMuc)
            {
                listDanhMuc.Add(Int32.Parse(DanhMuc));
            }

            String[] mangLinhVuc = mangInput.GetValues(Models.ChaoBan_Them_DichVu.Name_slLinhVuc);
            List<int> listLinhVuc = new List<int>();
            foreach (String LinhVuc in mangLinhVuc)
            {
                listLinhVuc.Add(Int32.Parse(LinhVuc));
            }

            canBanDAO.ThemDichVu(dv, dvcb, ttcg, listChuyenGiao, listCungCap, listDanhMuc, listLinhVuc);

            return DanhSachChaoBan();
        }

        [HttpPost]
        public ActionResult Them_ThietBiCongNghe(FormCollection mangInput)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            Models.ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            THONGTINCANBAN_DTO dv = new THONGTINCANBAN_DTO();
            //loai can ban: THietBiCongNghe = 1, DichVu = 2, GIaiPhapPhanMem = 3
            dv.LoaiCanBan = 1;
            dv.IDNguoiDang = (int) TK.ID1;

            dv.TenAnh = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_tenAnh];
            dv.TenViet = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_tenViet];
            dv.XuatXu = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_sllXuatXu];
            dv.MoTa = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_txtMoTa];
            dv.UuDiem = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_txtUuDiem];
            dv.HinhAnhDinhKem = "abc";
            dv.TepTinDinhKem = "abc";

            //phần thông tin thiết bị
            THONGTINTHIETBICONGNGHE_DTO tbcn = new THONGTINTHIETBICONGNGHE_DTO();
            tbcn.IDTieuChuan = Int32.Parse( mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputTieuChuan]);
            tbcn.ChiTieuKinhTeKyThuat = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_txtCacChiTieuKinhTeKyThuat];
            tbcn.CongSuatNangSuat = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_txtCongSuatNangSuat];
            tbcn.IDMucDoPhatTrien = Int32.Parse( mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputMucDoPhatTrien]);
            tbcn.IDXuatXuDeTai = Int32.Parse(mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputXuatXuDeTai]);
            tbcn.NamHoanThien = Int32.Parse( mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputNamHoanThien]);
            /////////////////////////////

            THONGTINLAPDAT_DTO ttld = new THONGTINLAPDAT_DTO();

            //phần yêu cầu kỹ thuật trong vận hành và lắp đặt
            ttld.LuongDienTieuThu = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputDien];
            ttld.LuongKhiDotDauTieuThu = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputKhiDot];
            ttld.LuongNhienLieuTieuThu = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputNhienLieu];
            ttld.MatBang = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputMatBang];
            ttld.SoCongNhan  = Int32.Parse( mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputCongNhan]);
            ttld.SoKySu  = Int32.Parse( mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputKySu]);
            ttld.SoKyThuatVien  =Int32.Parse(  mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputKyThuatVien]);
            ttld.SoNhanLuc= Int32.Parse( mangInput[Models.Kho_Them_ThietBiCongNghe.Name_inputNhanLuc]);



            //phần chuyển giao           
            String[] mangChuyenGiao = mangInput.GetValues(Models.Kho_Them_ThietBiCongNghe.Name_chuyenGiao);
            List<int> listChuyenGiao = new List<int>();
            foreach (String chuyenGiao in mangChuyenGiao)
            {
                listChuyenGiao.Add(Int32.Parse(chuyenGiao));
            }

            String[] mangCungCap = mangInput.GetValues(Models.Kho_Them_ThietBiCongNghe.Name_cungCap);
            List<int> listCungCap = new List<int>();
            foreach (String cungCap in mangCungCap)
            {
                listCungCap.Add(Int32.Parse(cungCap));
            }

            THONGTINCHUYENGIAOCANBAN_DTO ttcg = new THONGTINCHUYENGIAOCANBAN_DTO();
            ttcg.BaoHanh = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_baoHanh];
            ttcg.ChiPhiKhac = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_chiPhiKhac];
            ttcg.DanhSachDonViChuyenGiao = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_chuyenGiaoCho];
            ttcg.GiaKhoiDiem = mangInput[Models.Kho_Them_ThietBiCongNghe.Name_giaKhoiDiem] + " - " + mangInput[Models.Kho_Them_ThietBiCongNghe.Name_giaKetThuc]; ;

            //a = mangInput[Models.ChaoBan_Them_DichVu.Name_donviTinh];
            //a = mangInput[Models.ChaoBan_Them_DichVu.Name_donviTienTe];
            //a = mangInput[Models.ChaoBan_Them_DichVu.Name_txtDieuKienKhac];

            dv.TuKhoa = mangInput[Models.ChaoBan_Them_DichVu.Name_txttuKhoa];

            //thông tin phân loại
            String[] mangDanhMuc = mangInput.GetValues(Models.Kho_Them_ThietBiCongNghe.Name_slPhanLoai);
            List<int> listDanhMuc = new List<int>();
            foreach (String DanhMuc in mangDanhMuc)
            {
                listDanhMuc.Add(Int32.Parse(DanhMuc));
            }

            String[] mangLinhVuc = mangInput.GetValues(Models.Kho_Them_ThietBiCongNghe.Name_slLinhVuc);
            List<int> listLinhVuc = new List<int>();
            foreach (String LinhVuc in mangLinhVuc)
            {
                listLinhVuc.Add(Int32.Parse(LinhVuc));
            }

            canBanDAO.ThemThietBiCongNghe(dv, tbcn, ttld, ttcg, listChuyenGiao, listCungCap, listDanhMuc, listLinhVuc);

            return DanhSachChaoBan();
        }

        [HttpPost]
        public ActionResult Them_GiaiPhapPhanMem(FormCollection mangInput)
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }

            THONGTINCANBAN_DTO dv = new THONGTINCANBAN_DTO();
            //loai can ban: THietBiCongNghe = 1, DichVu = 2, GIaiPhapPhanMem = 3
            dv.LoaiCanBan = 1;

            //dv.MucDoPhatTrien = 
            dv.TenAnh = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_tenAnh];
            dv.TenViet = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_tenViet];
            dv.XuatXu = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_sllXuatXu];
            dv.MoTa = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_txtMoTa];
            dv.UuDiem = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_txtUuDiem];
            dv.HinhAnhDinhKem = "abc";
            dv.TepTinDinhKem = "abc";

            //phần thông tin thiết bị
            String[] mangTieuChuan = mangInput.GetValues(Models.Kho_Them_GiaiPhapPhanMem.Name_inputTieuChuan);
            String a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_txtCongSuatNangSuat];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_txtCacChiTieuKinhTeKyThuat];
            String[] mangMucDoPhatTrien = mangInput.GetValues(Models.Kho_Them_GiaiPhapPhanMem.Name_inputMucDoPhatTrien);
            String[] mangXuatXuDeTai = mangInput.GetValues(Models.Kho_Them_GiaiPhapPhanMem.Name_xuatXuDeTai);
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputNamHoanThien];

            //phần yêu cầu kỹ thuật trong vận hành và lắp đặt
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputNhanLuc];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputKySu];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputKyThuatVien];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputCongNhan];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputDien];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputKhiDot];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputMatBang];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputNhienLieu];


            //phần chuyển giao           
            String[] mangChuyenGiao = mangInput.GetValues(Models.Kho_Them_GiaiPhapPhanMem.Name_chuyenGiao);
            String[] mangCungCap = mangInput.GetValues(Models.Kho_Them_GiaiPhapPhanMem.Name_cungCap);

            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_giaKhoiDiem];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_giaKetThuc];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_donviTinh];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_donviTienTe];

            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_txtDieuKienKhac];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_chiPhiKhac];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_baoHanh];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_chuyenGiaoCho];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_donViDaChuyenGiao];

            //thông tin phân loại
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_slPhanLoai];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_slLinhVuc];
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_inputTuKhoa];


            //thông tin dịch vụ
            a = mangInput[Models.Kho_Them_GiaiPhapPhanMem.Name_txtDoiTuongSuDung];


            return Them_GiaiPhapPhanMem();
        }


        public ActionResult Them_GiaiPhapPhanMem()
        {
            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.ThemCanBan);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }

            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            List<Data_DAO_DTO_Lib.DTO.DTO> listPhanLoai = danhMucDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listLinhVuc = linhVucDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listChuyenGiao = chuyenGiaoDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listCungCap = cungCapDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listMucDoPhatTrien = mucDoPhatTrienDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listXuatXuDeTai = xuatXuDeTaiDAO.LayDanhSach();
            List<Data_DAO_DTO_Lib.DTO.DTO> listTieuChuan = tieuChuanDAO.LayDanhSach();
            Models.Kho_Them_GiaiPhapPhanMem model = new Models.Kho_Them_GiaiPhapPhanMem(listPhanLoai, listLinhVuc, listChuyenGiao, listCungCap, listMucDoPhatTrien, listXuatXuDeTai, listTieuChuan);
            return View("Kho_Them_GiaiPhapPhanMem", model);
        }

        Data_DAO_DTO_Lib.THONGDIEP_DAO tdDAO = new Data_DAO_DTO_Lib.THONGDIEP_DAO();
        public ActionResult DanhSachThongDiepChuaDoc(int startIndex = 0, int count = 20, string sorting = "")
        {
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ID1 == null)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = TK;

            List<Data_DAO_DTO_Lib.DTO.DTO> lTD = tdDAO.LayDanhSachThongDiepChuaDoc((int)TK.ID1, startIndex,  count, sorting);
            Kho_DanhSachThongDiepChuaDoc model = new Kho_DanhSachThongDiepChuaDoc(lTD);
            
             return View("Kho_DanhSachThongDiepChuaDoc", model);
        }

        public ActionResult DanhSachThongDiepDen(int startIndex = 0, int count = 20, string sorting = "")
        {
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ID1 == null)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = TK;
            List<Data_DAO_DTO_Lib.DTO.DTO> lTD = tdDAO.LayDanhSachThongDiepDen((int)TK.ID1, startIndex,  count, sorting);
            Kho_DanhSachThongDiepDen model = new Kho_DanhSachThongDiepDen(lTD);
             return View("Kho_DanhSachThongDiepDen", model);
        }

        public ActionResult DanhSachThongDiepDaGoi(int startIndex = 0, int count = 20, string sorting = "")
        {
            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ID1 == null)
            {
                //về trang chủ
                return RedirectToAction("Index", "Home");
            }
            
            ViewBag.TK = TK;
            List<Data_DAO_DTO_Lib.DTO.DTO> lTD = tdDAO.LayDanhSachThongDiepDaGoi((int)TK.ID1, startIndex,  count, sorting);
            Kho_DanhSachThongDiepDaGoi model = new Kho_DanhSachThongDiepDaGoi(lTD);
             return View("Kho_DanhSachThongDiepDaGoi", model);
        }        
         [HttpPost]
        public JsonResult DocThu(int id)
        {

            ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            if (TK.ID1 == null)
            {
                return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng đánh dấu thư đã đọc" });    
            }

            try
            {
                tdDAO.DanhDauThuDaDoc(id);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });    
            }

            return Json(new { Result = "OK"});    
            
        }
     [HttpPost]
         public JsonResult XoaRaoVatQuanTam(int id)
        {

            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng xoá quan tâm" });    

            }
         ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
         try 
	    {
            raovatDAO.xoaQuanTam(id, (int)TK.ID1);
	    }
             catch (Exception ex)
	    {
		
		    return Json(new { Result = "ERROR", Message = ex.Message });    
	    }
            
            return Json(new { Result = "OK"});    
            
        }
     [HttpPost]
     public JsonResult XoaTimMuaQuanTam(int id)
        {

            bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);

            if (isCoQuyen == false)
            {
                //về trang chủ
                return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng xoá quan tâm" });    

            }
         ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
         try 
	    {
            timmuaDAO.xoaQuanTam(id, (int)TK.ID1);
	    }
             catch (Exception ex)
	    {
		
		    return Json(new { Result = "ERROR", Message = ex.Message });    
	    }
            
            return Json(new { Result = "OK"});    
            
        }
     [HttpPost]
     public JsonResult XoaCanBanQuanTam(int id)
     {

         bool isCoQuyen = N03_11HCA_HTTTHD.TienIch.HeThong.KiemTraCoQuyenTruyCap(Session, N03_11HCA_HTTTHD.TienIch.QuyenTruyCap.QuanLyKho);

         if (isCoQuyen == false)
         {
             //về trang chủ
             return Json(new { Result = "ERROR", Message = "Không có quyền để thực hiện chức năng xoá quan tâm" });

         }
         ThongTinDanhNhapTaiKhoan TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
         try
         {
             canBanDAO.xoaQuanTam(id, (int)TK.ID1);
         }
         catch (Exception ex)
         {

             return Json(new { Result = "ERROR", Message = ex.Message });
         }

         return Json(new { Result = "OK" });

     }
        
    }
}

